{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "address = './face_detection.json'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import codecs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "409 image found!\n",
      "Sample row:\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'content': 'http://com.dataturks.a96-i23.open.s3.amazonaws.com/2c9fafb064277d86016431e33e4e003d/8186c3d1-e9d4-4550-8ec1-a062a7628787___0-26.jpg.jpeg',\n",
       " 'annotation': [{'label': ['Face'],\n",
       "   'notes': '',\n",
       "   'points': [{'x': 0.08615384615384615, 'y': 0.3063063063063063},\n",
       "    {'x': 0.1723076923076923, 'y': 0.45345345345345345}],\n",
       "   'imageWidth': 650,\n",
       "   'imageHeight': 333},\n",
       "  {'label': ['Face'],\n",
       "   'notes': '',\n",
       "   'points': [{'x': 0.583076923076923, 'y': 0.2912912912912913},\n",
       "    {'x': 0.6584615384615384, 'y': 0.46846846846846846}],\n",
       "   'imageWidth': 650,\n",
       "   'imageHeight': 333}],\n",
       " 'extras': None}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get links and stuff from json\n",
    "\n",
    "jsonData = []\n",
    "\n",
    "with codecs.open(address, 'rU', 'utf-8') as js:\n",
    "    for line in js:\n",
    "        jsonData.append(json.loads(line))\n",
    "\n",
    "print(f\"{len(jsonData)} image found!\")\n",
    "\n",
    "print(\"Sample row:\")\n",
    "\n",
    "jsonData[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import requests\n",
    "from tqdm import tqdm\n",
    "from PIL import Image\n",
    "from io import BytesIO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/409 [00:00<?, ?it/s]\n"
     ]
    },
    {
     "ename": "UnidentifiedImageError",
     "evalue": "cannot identify image file <_io.BytesIO object at 0x7f093b72fb50>",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mUnidentifiedImageError\u001b[0m                    Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[47], line 7\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[39mfor\u001b[39;00m data \u001b[39min\u001b[39;00m tqdm(jsonData):\n\u001b[1;32m      6\u001b[0m     response \u001b[39m=\u001b[39m requests\u001b[39m.\u001b[39mget(data[\u001b[39m'\u001b[39m\u001b[39mcontent\u001b[39m\u001b[39m'\u001b[39m])\n\u001b[0;32m----> 7\u001b[0m     img \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39masarray(Image\u001b[39m.\u001b[39;49mopen(BytesIO(response\u001b[39m.\u001b[39;49mcontent)))\n\u001b[1;32m      8\u001b[0m     images\u001b[39m.\u001b[39mappend([img, data[\u001b[39m\"\u001b[39m\u001b[39mannotation\u001b[39m\u001b[39m\"\u001b[39m]])\n",
      "File \u001b[0;32m~/.pyenv/versions/3.10.6/lib/python3.10/site-packages/PIL/Image.py:3283\u001b[0m, in \u001b[0;36mopen\u001b[0;34m(fp, mode, formats)\u001b[0m\n\u001b[1;32m   3281\u001b[0m     warnings\u001b[39m.\u001b[39mwarn(message)\n\u001b[1;32m   3282\u001b[0m msg \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mcannot identify image file \u001b[39m\u001b[39m%r\u001b[39;00m\u001b[39m\"\u001b[39m \u001b[39m%\u001b[39m (filename \u001b[39mif\u001b[39;00m filename \u001b[39melse\u001b[39;00m fp)\n\u001b[0;32m-> 3283\u001b[0m \u001b[39mraise\u001b[39;00m UnidentifiedImageError(msg)\n",
      "\u001b[0;31mUnidentifiedImageError\u001b[0m: cannot identify image file <_io.BytesIO object at 0x7f093b72fb50>"
     ]
    }
   ],
   "source": [
    "# load images from url and save into images\n",
    "\n",
    "images = []\n",
    "\n",
    "for data in tqdm(jsonData):\n",
    "    response = requests.get(data['content'])\n",
    "    img = np.asarray(Image.open(BytesIO(response.content)))\n",
    "    images.append([img, data[\"annotation\"]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "!mkdir face-detection-images\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "count = 1\n",
    "\n",
    "totalfaces = 0\n",
    "\n",
    "start = time.time()\n",
    "\n",
    "for image in images:\n",
    "    img = image[0]\n",
    "    metadata = image[1]\n",
    "    for data in metadata:\n",
    "        height = data['imageHeight']\n",
    "        width = data['imageWidth']\n",
    "        points = data['points']\n",
    "        if 'Face' in data['label']:\n",
    "            x1 = round(width*points[0]['x'])\n",
    "            y1 = round(height*points[0]['y'])\n",
    "            x2 = round(width*points[1]['x'])\n",
    "            y2 = round(height*points[1]['y'])\n",
    "            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 1)\n",
    "            totalfaces += 1\n",
    "    cv2.imwrite('./face-detection-images/face_image_{}.jpg'.format(count),img)\n",
    "    count += 1\n",
    "    \n",
    "end = time.time()\n",
    "\n",
    "print(\"Total test images with faces : {}\".format(len(images)))\n",
    "print(\"Sucessfully tested {} images\".format(count-1))\n",
    "print(\"Execution time in seconds {}\".format(end-start))\n",
    "print(\"Total Faces Detected {}\".format(totalfaces))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "face1 = cv2.imread(\"./face-detection-images/face_image_64.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(20,25))\n",
    "plt.imshow(face1)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(18,15))\n",
    "plt.imshow(cv2.cvtColor(face1, cv2.COLOR_BGR2RGB))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "face2 = cv2.imread(\"./face-detection-images/face_image_400.jpg\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(20,25))\n",
    "plt.imshow(face2)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d44d76ef8cbbc4331cecfe2e59228ac31ebb71026289858a116838be7168b60b"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
